package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.pojo.AdminTable;
import com.neuedu.pojo.Familymember;
import com.neuedu.pojo.Result;
import com.neuedu.service.FamilymemberService;
import com.neuedu.mapper.FamilymemberMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 * @description 针对表【familymember(家庭成员信息表)】的数据库操作Service实现
 * @createDate 2025-06-17 09:04:08
 */
@Service
public class FamilymemberServiceImpl extends ServiceImpl<FamilymemberMapper, Familymember>
        implements FamilymemberService{
    @Autowired
    private FamilymemberMapper familymemberMapper;
    @Override
    public Result<Familymember> getFamilymemberById(Integer id) {
        Familymember familymember = familymemberMapper.selectById(id);
        if (familymember !=null){
            return Result.success(familymember);
        }
        return Result.fail();
    }

    @Override
    public Result<List<Familymember>> getFamilymember(Integer currentPage, Integer pageSize) {
        // 设置条件
        Page<Familymember> page = new Page<>(currentPage,pageSize);
        // 设置条件查询
        LambdaQueryWrapper<Familymember> wrapper = new LambdaQueryWrapper<>();
        // 查询
        this.familymemberMapper.selectPage(page,wrapper);
        if (page.getRecords().size()>0){
            return Result.success(page.getRecords(),page.getTotal());
        }
        return Result.fail();
    }

    @Override
    public Result<Void> addFamilymember(Familymember familymember) {
        // 需要针对表中需要的数据，但是页面添加的时候没有提交的在这里补充
        familymember.setIsDelete(0);
        familymember.setCreateTime(new Date());
        familymember.setUpdateTime(new Date());
        // 添加
        int insert = this.familymemberMapper.insert(familymember);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> updateFamilymember(Familymember familymember) {
        // 需要针对表中需要的数据，但是页面添加的时候没有提交的在这里补充
        // familymember.setIsDelete(0);
        // familymember.setCreateTime(new Date());
        familymember.setUpdateTime(new Date());
        // 添加
        int insert = this.familymemberMapper.updateById(familymember);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> deleteFamilymember(Integer id) {
        LambdaUpdateWrapper<Familymember> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(Familymember::getIsDelete,1)
                .eq(Familymember::getMemberId,id);
        int update = this.familymemberMapper.update(wrapper);
        if (update > 0){
            return Result.success();
        }
        return Result.fail();
    }
}




